#******************************************************************
#
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# Notification Service build script
##

import platform
Import('env')

lib_env = env.Clone()
SConscript('#service/third_party_libs.scons', 'lib_env')
notification_env = lib_env.Clone()

if notification_env.get('RELEASE'):
    notification_env.AppendUnique(CCFLAGS=['-Os'])
else:
    notification_env.AppendUnique(CCFLAGS=['-g'])

target_os = env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
notification_env.AppendUnique(CPPPATH=[
    'include',
    'src/common',
    'src/provider',
    'src/consumer',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/c_common/oic_time/include',
    '#/resource/csdk/include',
    '#/resource/csdk/security/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/resource-directory/include',
    '#/resource/csdk/connectivity/api',
])

notification_env.PrependUnique(LIBS=[
    'resource_directory',
    'octbstack',
    'oc_logger',
    'connectivity_abstraction',
])

if target_os not in ['windows', 'winrt']:
    notification_env.AppendUnique(
        CCFLAGS=['-O2', '-g', '-Wall', '-fmessage-length=0'])

if target_os not in ['darwin', 'ios', 'windows', 'winrt']:
    notification_env.AppendUnique(LINKFLAGS=['-Wl,--no-undefined'])

notification_env.AddPthreadIfNeeded()

if target_os == 'android':
    notification_env.AppendUnique(CCFLAGS=['-frtti', '-fexceptions'])
    notification_env.AppendUnique(LIBS=['gnustl_shared', 'log'])

    if not env.get('RELEASE'):
        notification_env.AppendUnique(LIBS=['log'])

######################################################################
# Source files and Targets
######################################################################

notification_provider_env = notification_env.Clone()
notification_consumer_env = notification_env.Clone()

if target_os == 'android':
    notification_provider_env.AppendUnique(
        LINKFLAGS=['-Wl,-soname,libnotification_provider.so'])
if target_os == 'android':
    notification_consumer_env.AppendUnique(
        LINKFLAGS=['-Wl,-soname,libnotification_consumer.so'])

notification_common_obj = notification_provider_env.SharedObject(
    env.Glob('src/common/*.c'))

notification_provider_src = [
    env.Glob('src/provider/*.c'),
    notification_common_obj
]
notification_consumer_src = [
    env.Glob('src/consumer/*.c'),
    notification_common_obj
]

if target_os not in ['ios']:
    providersdk = notification_provider_env.SharedLibrary(
        'notification_provider', notification_provider_src)
    notification_provider_env.InstallTarget(
        providersdk, 'libnotification_provider')
    notification_provider_env.UserInstallTargetLib(
        providersdk, 'libnotification_provider')
    consumersdk = notification_consumer_env.SharedLibrary(
        'notification_consumer', notification_consumer_src)
    notification_consumer_env.InstallTarget(
        consumersdk, 'libnotification_consumer')
    notification_consumer_env.UserInstallTargetLib(
        consumersdk, 'libnotification_consumer')

providersdk = notification_provider_env.StaticLibrary(
    'notification_provider', notification_provider_src)
notification_provider_env.InstallTarget(
    providersdk, 'libnotification_provider')
notification_provider_env.UserInstallTargetLib(
    providersdk, 'libnotification_provider')

consumersdk = notification_consumer_env.StaticLibrary(
    'notification_consumer', notification_consumer_src)
notification_consumer_env.InstallTarget(
    consumersdk, 'libnotification_consumer')
notification_consumer_env.UserInstallTargetLib(
    consumersdk, 'libnotification_consumer')

notification_provider_env.UserInstallTargetHeader(
    'include/NSProviderInterface.h', 'service/notification', 'NSProviderInterface.h')
notification_consumer_env.UserInstallTargetHeader(
    'include/NSConsumerInterface.h', 'service/notification', 'NSConsumerInterface.h')
notification_consumer_env.UserInstallTargetHeader(
    'include/NSCommon.h', 'service/notification', 'NSCommon.h')

# Go to build Unit test
if notification_env.get('WITH_TEST') and target_os in ('linux'):
    SConscript('unittest/SConscript')

# Go to build c++ wrapper
SConscript('cpp-wrapper/SConscript')


if target_os == 'android':
    SConscript('android/SConscript')

# Go to build sample apps
SConscript('examples/SConscript')
